package com.njym.java.security.app.rest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njym.java.security.app.biz.CirclePostBiz;
import com.njym.java.security.app.biz.PostCommentBiz;
import com.njym.java.security.app.entity.CirclePost;
import com.njym.java.security.app.entity.PostCommentVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

@Controller
@RequestMapping("circlePost")
public class CirclePostController extends BaseController<CirclePostBiz, CirclePost> {

   @Autowired
   private PostCommentBiz postCommentBiz;

    /**
     * @param status 是否精华帖
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
                                    String ircleId,String status) {
        if (!ircleId.equals("undefined") && !status.equals("undefined")) {
            Page<Object> result;
            if(Integer.parseInt(status) == 0){
                Example example = new Example(CirclePost.class);
                example.createCriteria().andEqualTo("fid",Integer.parseInt(ircleId)).andEqualTo("deleted",0).andEqualTo("invisible",1);
                example.orderBy("dateline").desc();
                result = PageHelper.startPage(page, limit);
                baseBiz.selectByExample(example);
            }else {
                Example example = new Example(CirclePost.class);
                example.createCriteria().andEqualTo("fid",Integer.parseInt(ircleId)).andEqualTo("statuses",Integer.parseInt(status)).andEqualTo("deleted",0).andEqualTo("invisible",1);
                example.orderBy("dateline").desc();
                result = PageHelper.startPage(page, limit);
                baseBiz.selectByExample(example);
            }
            return new TableResultResponse<>(result.getPages(),result.getResult());
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }


    /**
     * 点赞
     * @param id id
     */
    @RequestMapping(value = "/addLike", method = RequestMethod.GET)
    @ResponseBody
    public Object add(String id) {
        if (!id.equals("undefined")) {
            baseBiz.addLike(Integer.parseInt(id));
            return new ObjectRestResponse();
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    /**
     * 查询我的帖子-审核
     * @param limit
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping(value = "/toExamine", method = RequestMethod.GET)
    @ResponseBody
    public Object toExamine(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
                                                        String userId){
        if (!userId.equals("undefined")) {
            Page<Object> result = PageHelper.startPage(page, limit);
            baseBiz.toExamine(Integer.parseInt(userId));
            return new TableResultResponse<>(result.getPages(),result.getResult());
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

    /**
     * 查询我的帖子-发布
     * @param limit
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping(value = "/release", method = RequestMethod.GET)
    @ResponseBody
    public Object release(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
                             String userId){
        if (!userId.equals("undefined")) {
            Page<Object> result = PageHelper.startPage(page, limit);
            baseBiz.release(Integer.parseInt(userId));
            return new TableResultResponse<>(result.getPages(),result.getResult());
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }
}